package com.ywjh.inter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.ywjh.inter.entity.ProfileEntity;

public class ProfileDAO {

	public int insert(Connection conn, ProfileEntity entity) throws SQLException {
		int id = -1;
		PreparedStatement ps=conn.prepareStatement("insert into profile( user_id, name, idcard, high, weight, disease_id, his_disease, update_time, status,add_time,bmi,default_address_id,memo,gender,brithday,group_disease,has_new_insulin_order) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
		ps.setInt(1, entity.getUser_id());
		ps.setString(2, entity.getName());
		ps.setString(3, entity.getIdcard());
		ps.setFloat(4, entity.getHigh());
		ps.setFloat(5, entity.getWeight());
		ps.setInt(6, entity.getDisease_id());
		ps.setString(7, entity.getHis_disease());
		ps.setTimestamp(8, entity.getUpdate_time());
		ps.setInt(9, entity.getStatus());
		ps.setTimestamp(10, entity.getAdd_time());
		ps.setInt(11, entity.getBmi());
		ps.setInt(12, entity.getDefault_address_id());
		ps.setString(13, entity.getMemo());
		ps.setInt(14, entity.getGender());
		ps.setString(15, entity.getBrithday());
		ps.setString(16, entity.getGroup_disease());
		ps.setInt(17, entity.getHasNewInsulinOrder());
		if (ps.executeUpdate() > 0) {
			ResultSet rs = ps.getGeneratedKeys();
			if (rs.next()) {
				id = rs.getInt(1);
			}
		}
		return id;
	}
	public boolean update(Connection conn, ProfileEntity entity) throws SQLException {
		boolean b=false;
		PreparedStatement ps=conn.prepareStatement("update profile set  user_id=?, name=?, idcard=?, high=?, weight=?, disease_id=?, his_disease=?, update_time=?, status=?,add_time=?,bmi=?,default_address_id=?,memo=?,gender=?,brithday=?,group_disease=?,has_new_insulin_order=? where id =?");
		ps.setInt(1, entity.getUser_id());
		ps.setString(2, entity.getName());
		ps.setString(3, entity.getIdcard());
		ps.setFloat(4, entity.getHigh());
		ps.setFloat(5, entity.getWeight());
		ps.setInt(6, entity.getDisease_id());
		ps.setString(7, entity.getHis_disease());
		ps.setTimestamp(8, entity.getUpdate_time());
		ps.setInt(9, entity.getStatus());
		ps.setTimestamp(10, entity.getAdd_time());
		ps.setInt(11, entity.getBmi());
		ps.setInt(12, entity.getDefault_address_id());
		ps.setString(13, entity.getMemo());
		ps.setInt(14, entity.getGender());
		ps.setString(15, entity.getBrithday());
		ps.setString(16, entity.getGroup_disease());
		ps.setInt(17,entity.getHasNewInsulinOrder());
		ps.setInt(18, entity.getId());
		if (ps.executeUpdate() > 0) {
			b=true;
		}
		return b;
	}
	public ProfileEntity findByUserId(Connection conn, int user_id) throws SQLException {
		ProfileEntity e=null;
		PreparedStatement ps=conn.prepareStatement("select * from profile where user_id=?");
		ps.setInt(1, user_id);
		ResultSet rs=ps.executeQuery();
		if(rs.next()) {
			e=fill(rs);
		}
		return e;
	}
	private ProfileEntity fill(ResultSet rs) throws SQLException {
		ProfileEntity e=new ProfileEntity();
		e.setId(rs.getInt("id"));
		e.setDisease_id(rs.getInt("disease_id"));
		e.setHigh(rs.getFloat("high"));
		e.setHis_disease(rs.getString("his_disease"));
		e.setIdcard(rs.getString("idcard"));
		e.setName(rs.getString("name"));
		e.setStatus(rs.getInt("status"));
		e.setUpdate_time(rs.getTimestamp("update_time"));
		e.setUser_id(rs.getInt("user_id"));
		e.setWeight(rs.getFloat("weight"));
		e.setAdd_time(rs.getTimestamp("add_time"));
		e.setBmi(rs.getInt("bmi"));
		e.setDefault_address_id(rs.getInt("default_address_id"));
		e.setMemo(rs.getString("memo"));
		e.setGender(rs.getInt("gender"));
		e.setBrithday(rs.getString("brithday"));
		e.setGroup_disease(rs.getString("group_disease"));
		e.setHasNewInsulinOrder(rs.getInt("has_new_insulin_order"));
		return e;
	}
}
